﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Arizu.Repository;
using System.Data;
using System.Text;

namespace AiRiZu
{
    public partial class PersonList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sql = "select * from PersonInfo where Isdelete=0";
                bind(sql);
            }
        }

        private void bind(string sql)
        {
            DataTable dt = Main.CreateInstance().GetAllRoom(sql);
            SignDg.DataSource = dt;
            SignDg.DataBind();
        }

        protected void SignDg_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            string id = e.Item.Cells[0].Text;
            string sqlDel = "update PersonInfo set Isdelete=1 where PersonId=" + Convert.ToInt16(id);
            Main.CreateInstance().DelPerson(sqlDel);
            string sql = "select * from PersonInfo where Isdelete=0";
            this.bind(sql);
        }

        protected void SignDg_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int index = int.Parse(e.Item.Cells[0].Text);
            Response.Redirect("ExitPerson.aspx?PersonId=" + index);
        }

        protected void SignDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.SignDg.CurrentPageIndex = e.NewPageIndex;
            string sql = "select * from PersonInfo where Isdelete=0";
            bind(sql);
        }

        protected void sel_Click(object sender, EventArgs e)
        {
            string where = " and ";
            if (!string.IsNullOrEmpty(this.username.Text))
            {
                where += "Name='" + this.username.Text + "' and ";
            }
            if (!string.IsNullOrEmpty(this.useremail.Text))
            {
                where += "Email='" + this.useremail.Text + "' and ";
            }
            where += "Sex='" + this.sex.SelectedValue + "'";
            string sql = "select * from PersonInfo where Isdelete=0" + where;
            bind(sql);
        }
    }
}
